# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('gdbm' 'libgdbm' 'libgdbm-devel')
pkgver=1.18.1
pkgrel=2
pkgdesc="GNU database library"
url="https://www.gnu.org/software/gdbm/gdbm.html"
license=('GPL3')
arch=('i686' 'x86_64')
makedepends=('libreadline-devel')
source=(https://ftp.gnu.org/gnu/gdbm/${pkgname}-${pkgver}.tar.gz{,.sig}
        gdbm-1.10-zeroheaders.patch
        1.10-no-undefined.patch
        1.11-parallel-make.patch
        gdbm-1.11-msysize.patch)
options=('!makeflags')
sha256sums=('86e613527e5dba544e73208f42b78b7c022d4fa5a6d5498bf18c8d6f745b91dc'
            'SKIP'
            '0401f8026f4673ea1be50bfd3bfa6c6b3bddd8539733d58911336c7908df452f'
            'b2d661b1365fde38e4ed1796433767ed61b3be72f3b6c48b3d27e4004db01877'
            '4a3bbb05aa085be08e9a1c2e14ddd99d7b04423a18b0e5e9d6d82bc826133610'
            '4ccc9d37fd0d93d16f8d9f9c6ac4fb548643fa732f2c73e669751f3e9ba3401f')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  # Prevent gdbm from storing uninitialized memory content
  # to database files. This patch improves security, as the
  # uninitialized memory might contain sensitive informations
  # from other applications.
  # https://bugzilla.redhat.com/show_bug.cgi?id=4457
  # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=208927
  patch -Np1 -i ${srcdir}/gdbm-1.10-zeroheaders.patch

  patch -Np2 -i ${srcdir}/1.10-no-undefined.patch
  patch -Np2 -i ${srcdir}/1.11-parallel-make.patch
  patch -Np1 -i ${srcdir}/gdbm-1.11-msysize.patch

  autoreconf -fi
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --enable-libgdbm-compat
  make
  make DESTDIR=${srcdir}/dest install
}

check() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make check
}

package_gdbm() {
  depends=("libgdbm=${pkgver}")
  groups=('Database')
  install=gdbm.install

  mkdir -p ${pkgdir}/usr/{bin,share}

  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  #cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/info ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/locale ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/man ${pkgdir}/usr/share/
}

package_libgdbm() {
  depends=('gcc-libs' 'libreadline')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libgdbm-devel() {
  pkgdesc="libgdbm headers and libraries"
  options=('staticlibs')
  depends=("libgdbm=${pkgver}" 'libreadline-devel')
  groups=('development')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
